/*
 * ViewController
 * @description 本文件提供友盟分享的单例
 * @package
 * @author 		zyf
 * @copyright
 * @version 		***
 * @description 本文件提供友盟分享的单例
 */
#import "BanBu_UMengShareManager.h"
#import "UMSocial.h"
#import "UMSocialQQHandler.h"
#import "UMSocialWechatHandler.h"
#define kVideoPath @"http://www.halfarea.com/shareto/play_mp4.html?fileurl=%@"
#define kImagePath @"http://www.halfarea.com/shareto/play_jpg.html?fileurl=%@"
@implementation BanBu_UMengShareManager

static BanBu_UMengShareManager * umManager=nil;
+(BanBu_UMengShareManager *)shareUMengManager
{
    if (umManager ==nil) {
        umManager=[[BanBu_UMengShareManager alloc]init];
    }
    return umManager;
}

#pragma mark -
/**
 * @函数名称：shareToPlatform:WithText:WithImage:WithClickUrlStr:WithPerformVC:
 * @函数描述：用来给友盟分享sdk传所需参数
 * @输入参数：platform text image url presentVC
 * @输出参数：
 * @返回值：
 * @author：your name
 */
-(void)shareToPlatform:(SharePlatform )platform WithText:(NSString *)text WithImage:(UIImage  *)image WithClickUrlStr:(NSString *)url WithPerformVC:(UIViewController *)presentVC
{
    [UMSocialConfig setFinishToastIsHidden:NO position:UMSocialiToastPositionCenter];
    NSMutableString * muStr=[NSMutableString stringWithFormat:@"%@",url];
    NSString *urlType=[[muStr componentsSeparatedByString:@"."] lastObject];
    NSString *urlStr;
    
    NSMutableString *muText = [[NSMutableString alloc]init];
    
    NSData * videoPath = [url dataUsingEncoding:NSUTF8StringEncoding];
    NSString * videoStr = [videoPath base64EncodedString];
    //去掉base64编码后的空行
    NSArray *array=[videoStr componentsSeparatedByString:@"\r\n"];
    for (NSString *ss in array)
    {
        [muText appendString:ss];
    }
    if ([urlType isEqualToString:@"mp4"]||[urlType isEqualToString:@"avi"])
    {
        urlStr=[NSString stringWithFormat:kVideoPath,muText];
    }
    else if ([urlType isEqualToString:@"jpg"]||[urlType isEqualToString:@"png"])
    {
        urlStr = [NSString stringWithFormat:kImagePath,muText];
    }
    else
    {
        urlStr=url;
    }
    if (url==nil||[url isEqualToString:@""]) {
        urlStr=@"www.halfeet.cn";
    }
    if (text==nil||[text isEqualToString:@""]) {
        text=@" ";
    }
    
    //分享平台
    switch (platform) {
        case 0:
        {//sina
            NSString *titleStr=[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"share_broadcast", nil),NSLocalizedString(@"sinaSheetTitle",nil)];
            
            [UMSocialConfig setNavigationBarConfig:^(UINavigationBar *bar,UIButton *closeButton,UIButton *backButton,UIButton *postButton,UIButton *refreshButton,UINavigationItem * navigationItem){
                
                UILabel *titleLab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
                titleLab.text=titleStr;
                titleLab.font=[UIFont systemFontOfSize:13];
                navigationItem.titleView=titleLab;
                
            }];
            if ([urlType isEqualToString:@"mp4"]||[url isEqualToString:@"avi"]) {
                [UMSocialData defaultData].extConfig.sinaData.shareText=urlStr;
            }
            else
            {
                [UMSocialData defaultData].extConfig.sinaData.shareText=text;
                
            }
            [[UMSocialControllerService defaultControllerService] setShareText:text shareImage:image socialUIDelegate:nil];        //设置分享内容和回调对象
            [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina].snsClickHandler(presentVC,[UMSocialControllerService defaultControllerService],YES);
        }
            break;
        case 1:
        {
            NSString *titleStr=[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"share_broadcast", nil),NSLocalizedString(@"TXSheetTitle", nil)];
            [UMSocialConfig setNavigationBarConfig:^(UINavigationBar *bar,UIButton *closeButton,UIButton *backButton,UIButton *postButton,UIButton *refreshButton,UINavigationItem * navigationItem){
                
                UILabel *titleLab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 30)];
                titleLab.text=titleStr;
                titleLab.font=[UIFont systemFontOfSize:12];
                navigationItem.titleView=titleLab;
                
            }];
            if ([urlType isEqualToString:@"mp4"]||[urlType hasSuffix:@"rm"]) {
                [UMSocialData defaultData].extConfig.tencentData.shareText=urlStr;
                
            }
            else
                [UMSocialData defaultData].extConfig.tencentData.shareText=text;
            
            //腾讯微博
            [[UMSocialControllerService defaultControllerService] setShareText:text shareImage:image socialUIDelegate:nil];        //设置分享内容和回调对象
            [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToTencent].snsClickHandler(presentVC,[UMSocialControllerService defaultControllerService],YES);
        }
            break;
        case 2:
        {//qzone
            [UMSocialData defaultData].extConfig.qzoneData.title=NSLocalizedString(@"reginShare", nil);
            [UMSocialData defaultData].extConfig.qzoneData.shareText=text;
            [UMSocialQQHandler setQQWithAppId:@"801238036" appKey:@"53d5c27656240b687d01b691" url:urlStr];
            [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToQzone] content:text image:image location:nil urlResource:nil presentedController:presentVC completion:^(UMSocialResponseEntity *response){
                if (response.responseCode == UMSResponseCodeSuccess) {
                    NSLog(@"分享成功！");
                }
            }];
        }
            break;
        case 3:
        {//wxtl
            
            if (!url && (!image))
            {
                [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeText;
            }
//            else if([urlType isEqualToString:@"jpg"]||[urlType isEqualToString:@"png"]||url==nil)
//            {
//                [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeImage;
//    
//            }
            else if ([urlType isEqualToString:@"mp4"]||[urlType hasSuffix:@"avi"])
            {
                [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
                [UMSocialData defaultData].extConfig.wechatTimelineData.url = urlStr;
            }
            else
            {
                [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
                //设置微信好友或者朋友圈的分享url,下面是微信好友，微信朋友圈对应wechatTimelineData
                [UMSocialData defaultData].extConfig.wechatTimelineData.url = urlStr;
            }
            [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatTimeline] content:text image:image location:nil urlResource:nil presentedController:presentVC completion:^(UMSocialResponseEntity *response){
                if (response.responseCode == UMSResponseCodeSuccess) {
                    NSLog(@"分享成功！");
                    
                }
            }];
        }
            break;
        case 4:
        {//wxsession
            if (!url && (!image))
            {
                [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeText;
            }
            else if([urlType isEqualToString:@"jpg"]||[urlType isEqualToString:@"png"]||url==nil)
            {
                [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeImage;
            }
            else if ([urlType isEqualToString:@"mp4"]||[urlType hasSuffix:@"rm"])
            {
                [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
                [UMSocialData defaultData].extConfig.wechatSessionData.url=urlStr ;
            }
            else {
                [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
                //设置微信好友或者朋友圈的分享url,下面是微信好友，微信朋友圈对应wechatTimelineData
                [UMSocialData defaultData].extConfig.wechatSessionData .url =urlStr;
            }
            
            [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatSession] content:text image:image location:nil urlResource:nil presentedController:presentVC completion:^(UMSocialResponseEntity *response){
                if (response.responseCode == UMSResponseCodeSuccess) {
                    NSLog(@"分享成功！");
                    
                }
            }];
            
        }
            break;
        case 5:
        {//renren
#warning 国际化
            NSString *titleStr=[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"share_broadcast", nil),NSLocalizedString(@"renren", nil)];
            [UMSocialConfig setNavigationBarConfig:^(UINavigationBar *bar,UIButton *closeButton,UIButton *backButton,UIButton *postButton,UIButton *refreshButton,UINavigationItem * navigationItem){
                
                UILabel *titleLab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 30)];
                titleLab.text=titleStr;
                titleLab.font=[UIFont systemFontOfSize:12];
                navigationItem.titleView=titleLab;
            }];
            if ([urlType isEqualToString:@"mp4"]||[urlType hasSuffix:@"avi"]) {
                [UMSocialData defaultData].extConfig.renrenData.shareText=urlStr;
            }
            else
                [UMSocialData defaultData].extConfig.renrenData.shareText=text;
            
            [[UMSocialControllerService defaultControllerService] setShareText:text shareImage:image socialUIDelegate:nil];        //设置分享内容和回调对象
            [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToRenren].snsClickHandler(presentVC,[UMSocialControllerService defaultControllerService],YES);
            if ([text isEqualToString:@""]) {
                [UMSocialData defaultData].extConfig.renrenData.shareText =@"  ";
            }
        }
            break;
        case 6:
        {//facebook
            NSString *titleStr=[NSString stringWithFormat:@"%@Facebook",NSLocalizedString(@"share_broadcast", nil)];
            [UMSocialConfig setNavigationBarConfig:^(UINavigationBar *bar,UIButton *closeButton,UIButton *backButton,UIButton *postButton,UIButton *refreshButton,UINavigationItem * navigationItem){
                
                UILabel *titleLab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 30)];
                titleLab.text=titleStr;
                titleLab.font=[UIFont systemFontOfSize:12];
                navigationItem.titleView=titleLab;
            }];
            if ([urlType isEqualToString:@"mp4"]||[urlType isEqualToString:@"avi"]) {
                [UMSocialData defaultData].extConfig.facebookData.shareText=urlStr;
            }
            else
            {
                NSRange range= [url rangeOfString:@"_broad_cast"];
                if (range.length) {
                    text=[NSString stringWithFormat:@"%@ %@",text,urlStr];
                }
                [UMSocialData defaultData].extConfig.facebookData.shareText=text;
            }
            [[UMSocialControllerService defaultControllerService] setShareText:text shareImage:image socialUIDelegate:nil];        //设置分享内容和回调对象
            [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToFacebook].snsClickHandler(presentVC,[UMSocialControllerService defaultControllerService],YES);
        }
            break;
        case 7:
        {//twitter
            NSString *titleStr=[NSString stringWithFormat:@"%@Twitter",NSLocalizedString(@"share_broadcast", nil)];
            [UMSocialConfig setNavigationBarConfig:^(UINavigationBar *bar,UIButton *closeButton,UIButton *backButton,UIButton *postButton,UIButton *refreshButton,UINavigationItem * navigationItem){
                
                UILabel *titleLab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 30)];
                titleLab.text=titleStr;
                titleLab.font=[UIFont systemFontOfSize:12];
                navigationItem.titleView=titleLab;
            }];
            
            if ([urlType isEqualToString:@"mp4"]||[urlType isEqualToString:@"avi"]) {
                [UMSocialData defaultData].extConfig.twitterData.shareText=urlStr;
            }
            else
                [UMSocialData defaultData].extConfig.twitterData.shareText=text;
            
            [[UMSocialControllerService defaultControllerService] setShareText:text shareImage:image socialUIDelegate:nil];        //设置分享内容和回调对象
            [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToTwitter].snsClickHandler(presentVC,[UMSocialControllerService defaultControllerService],YES);
        }
            break;
          //分享到QQ好友
        case ShareToQQ:
        {
            [UMSocialData defaultData].extConfig.qzoneData.title=NSLocalizedString(@"reginShare", nil);
            [UMSocialData defaultData].extConfig.qzoneData.shareText=text;
            [UMSocialQQHandler setQQWithAppId:@"801238036" appKey:@"53d5c27656240b687d01b691" url:urlStr];
            [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToQQ] content:text image:image location:nil urlResource:nil presentedController:presentVC completion:^(UMSocialResponseEntity *response){
                if (response.responseCode == UMSResponseCodeSuccess) {
                    NSLog(@"分享成功！");
                }}];
            
        }
        default:
            break;
    }
    
}
@end
